import 'package:intl/intl.dart';

class SPUtils {
  ///前n天的时间戳
  static getSubtractDay(int num) {
    // 获取当前时间
    DateTime now = DateTime.now();

    // 获取前 n 天的时间
    DateTime fifteenDaysAgo = now.subtract(Duration(days: num));

    // 将前 n 天的时间转换为时间戳
    int timestamp = fifteenDaysAgo.millisecondsSinceEpoch ~/ 1000;
    return timestamp;
  }

  ///前n天的时间
  static List<String> getDatesFromLastDays(int num,
      {String attern = 'yyyy-MM-dd'}) {
    DateTime now = DateTime.now();
    List<String> dates = [];

    for (int i = 0; i <= num; i++) {
      DateTime date = now.subtract(Duration(days: i));
      String formattedDate = DateFormat(attern).format(date);
      dates.add(formattedDate);
    }

    return dates.reversed.toList();
  }

  ///获取时间字符串
  static String getTimeStr({String attern = 'yyyy-MM-dd', DateTime? time}) {
    DateTime now = DateTime.now();
    DateFormat(attern).format(time ?? now);
    return DateFormat(attern).format(time ?? now);
  }

  ///获取时间戳字符串
  static getTimeInt(String tiemStr) {
    int timestamp = DateTime.parse(tiemStr).millisecondsSinceEpoch ~/ 1000;
    return timestamp;
  }

  static timeIntToTimeStr(int timestamp, {String attern = 'yyyy-MM-dd'}) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
    return DateFormat(attern).format(dateTime);
  }

  static Duration timestampDifference(int star, int end) {
    DateTime startDateTime = DateTime.fromMillisecondsSinceEpoch(star * 1000);
    DateTime endDateTime = DateTime.fromMillisecondsSinceEpoch(end * 1000);
    Duration difference = endDateTime.difference(startDateTime);
    return difference;
  }

  static bool validatePassword(String password) {
    // 长度要求
    if (password.length < 8 || password.length > 16) {
      return false;
    }

    // 正则表达式
    RegExp digitRegex = RegExp(r'[0-9]');
    RegExp letterRegex = RegExp(r'[A-Za-z]');
    RegExp symbolRegex = RegExp(r'[^A-Za-z0-9]');

    // 判断至少有两种不同的字符类型
    int count = 0;
    if (digitRegex.hasMatch(password)) count++;
    if (letterRegex.hasMatch(password)) count++;
    if (symbolRegex.hasMatch(password)) count++;

    return count >= 2;
  }
}
